/**
 * 
 */
package com.yst.metho.web.application.projectversion.renderer;

import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.Component;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Table;
import nextapp.echo2.app.table.TableCellRenderer;

import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.activities.Activity;
import com.yst.suivi.data.user.User;
import com.yst.suivi.status.StatusUtil;

/**
 * @author perso
 * 
 */
public class ProjectWBSTableRenderer implements TableCellRenderer
{

	public static final int NAME_COLUMN = 0;

	public static final int DURATION_COLUMN = 1;

	public static final int FOLDER_COLUMN = 2;

	public static final int USER_COLUMN = 3;

	public static final int STATUS_COLUMN = 4;

	public static final int LOT_COLUMN = 5;

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(nextapp.echo2.app.Table,
	 *      java.lang.Object, int, int)
	 */
	public Component getTableCellRendererComponent(Table table, Object value, int column, int row)
	{
		Activity act = (Activity) value;
		Label label = new Label("");
		if (null != value)
		{
			switch (column)
			{
			case NAME_COLUMN:
				label.setText(act.getNom());
				break;
			case DURATION_COLUMN:
				label.setText(act.getDuree().toString());
				break;
			case FOLDER_COLUMN:
				label.setText(act.getFolder().getNom());
				break;
			case USER_COLUMN:
				if (null != act.getUser())
				{
					try
					{
						Iterator it = Locator.getActivityView().getAffectedUser(act).iterator();
						User usr;
						StringBuffer buf = new StringBuffer();
						boolean isFirst = true;
						while (it.hasNext())
						{
							usr = (User) it.next();
							if (!isFirst)
							{
								buf.append(", ");
							}
							else
							{
								isFirst = false;
							}
							buf.append(usr.getFullname());
						}
						label.setText(buf.toString());
					}
					catch (CreateException e)
					{
						ExceptionMgt.manage(e);
					}
					catch (NamingException e)
					{
						ExceptionMgt.manage(e);
					}
					catch (FinderException e)
					{
						ExceptionMgt.manage(e);
					}
				}
				break;
			case STATUS_COLUMN:
				label.setText(StatusUtil.getStatusName(act.getStatus()));
				break;
			case LOT_COLUMN:
				if (null != act.getLot())
				{
					label.setText(act.getLot().getNom());
				}
				break;
			default:
				label.setText("colonne inconnu : " + column);
			}
		}
		else
		{
			label.setText("null");
		}
		if (row % 2 == 0)
		{
			label.setStyleName("ListingView.EvenRowLabel");
		}
		else
		{
			label.setStyleName("ListingView.OddRowLabel");
		}
		return label;
	}

}
